package com.leon.notepad.content.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtils {

    //Date转字符串
    public static String getStrDate(Date date){
        String timeString = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
        timeString = sdf.format(date);//单位秒
        return timeString;
    }

    /**
     * 格式化long类型的时间戳
     */
    public static String getCurrentTime(long dates) {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sDateFormat.format(new Date(dates));
        return date;
    }
    /**
     * 格式化Date时间
     */
    public static String getTimes(Date date) {//年月日时分秒格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    /**
     * 获取今天0点时间
     * @return
     */
    public static long getTodayTime(){
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTimeInMillis();
    }

    /**
     * 获取传入时间月份第一天
     */
    public static Date getFirstDayDateOfMonth(Date date) {
        // 获取指定日期所在月的第一天
        Calendar calendar = Calendar.getInstance();
        // 设置指定日期
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }
    /**
     * 获取当前月最后一天最后一秒
     */
    public static Date getLastDayOfMonth(Date date) {
        // 获取指定日期所在月的最后一天
        Calendar calendar = Calendar.getInstance();
        // 设置指定日期
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
        // 最后一天
        calendar.roll(Calendar.DATE, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }
}
